<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>

        <!-- 导航栏 -->
        <HomePageNavigation1></HomePageNavigation1>

        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adImg1" v-if="adImg1"></HomeTopTen>

        <!-- 二级标题-->
        <div class="sannongzhichuang" v-if="parent_name != ''">
            <div class="inner">
                <h2>
                    <NuxtLink :to="`/primaryNavigation/${parent_id}`"> {{ parent_name }}</NuxtLink>
                    <em class="iconfont icon-xingzhuang-zhijiaosanjiaoxing-copy"></em>
                </h2>
                <p class="introduction">
                    <strong>频道介绍</strong>
                <div v-for="(item, index) in secondNav" :key="index">
                    <span v-if="index <= 7">
                        <NuxtLink :to="{ path: `/newsList/${item.category_id}` }">{{ item.alias }}</NuxtLink>
                    </span>
                    <b v-else></b>
                </div>
                </p>
            </div>
        </div>

        <!-- 面包屑导航 -->
        <div class="breadcrumb">
            <div class="inner">
                <span class="location">当前位置:</span>
                <el-breadcrumb :separator-icon="ArrowRight">
                    <el-breadcrumb-item>
                        <NuxtLink to="/">首页</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item v-if="parent_name != ''">
                        <NuxtLink :to="`/primaryNavigation/${parent_id}`"> {{ parent_name }}</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <ul class="list">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :to="item.linkurl" v-show="item.islink == 1" :title="item.title">{{ item.title }}
                            </NuxtLink>
                            <NuxtLink :to="`/newsDetail/${item.id}`" v-if="item.islink == 0" :title="item.title">{{
                                item.title }}</NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" v-if="total">
                        <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                            v-model:page-size="pageSize" prev-text="上一页" next-text="下一页" @change="changePage" />
                    </div>
                </div>
                <div class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </div>
            </div>
        </div>

        <!-- 列表页广告二 -->
        <HomeTopTen :imgurl="adImg2" v-if="adImg2"></HomeTopTen>

        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//1.页面必备依赖 start ---------------------------------------->
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
//获得跳转过来的id
const route = useRoute();
const articleId = route.params.id; //获得该页面的id
const category_id = route.query.catid; //获得该页面的id
//当前列表名称
const name = ref('')

const { $webUrl, $CwebUrl, $BwebUrl } = useNuxtApp()
//1.页面必备依赖 end ---------------------------------------->


//2.页面数据 start ---------------------------------------->
let id = useState("id", () => "")
let total = useState("total", () => 0)
let page = useState("page", () => 1)
let pageSize = useState("pageSize", () => 20)

id.value = route.query.id;

//2.1 获得所有广告
//广告列表
let adImg1 = ref({});
let adImg2 = ref({});
async function getAdData() {
    const adData = await requestDataPromise('/web/getWebsiteAdvertisement', { method: 'GET', query: { 'ad_tag': 'LIST' } });
    if (adData.code == 200) {
        for (let item of adData.data) {
            if (item.ad_tag == 'LIST_0001') {
                adImg1.value = item;
            }
            if (item.ad_tag == 'LIST_0002') {
                adImg2.value = item;
            }
        }
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取广告列表")
        console.log("后端错误反馈：", adData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
getAdData();

//2.2新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData = await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        newsList.value = listData.data.rows;
        total.value = listData.data.count;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
newslists();

//分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    page.value = value
    console.log(page.value);
    newslists()
}

//2.3获得页面名称
let getPageName = async () => {
    const pageName = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (pageName.code == 200) {
        name.value = pageName.data.alias
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：设置页面标题")
        console.log("后端错误反馈：", pageName.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
getPageName();

//2.页面数据 end ---------------------------------------->


//3.二级栏目 start ---------------------------------------->
//3.1通过id获取父栏目
const parent_name = ref([]);
const parent_id = ref([]);
let getParentNav = async () => {
    const listData = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        console.log(listData.data);
        parent_name.value = listData.data.parent_name;
        parent_id.value = listData.data.parent_id;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
    getSecondNav();
}
//获得列表
getParentNav();

// 3.2获取二级栏目
const secondNav = ref([]);
let getSecondNav = async () => {
    const listData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': parent_id.value,
            'num': 8,
        },
    });
    console.log('listData', listData);

    if (listData.code == 200) {
        secondNav.value = listData.data;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//3.二级栏目 end ---------------------------------------->

//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': articleId
    },
});

if (setData.code == 200) {
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoName + "_" + seoSuffix,
        meta: [
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix },
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix }
        ]
    });
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：设置列表页面SEO数据")
    console.log("后端错误反馈：", setData.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}


//4.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import url('@/assets/css/list.less');
</style>